package com.srwl.mytx.receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

import com.srwl.mytx.MyApplication;
import com.srwl.mytx.RetrofitManager;
import com.srwl.mytx.converter.MyRequestBodyConverter;
import com.srwl.mytx.domain.CommonResult;
import com.srwl.mytx.service.UserService;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

@Deprecated
public class TimeChangeReceiver extends BroadcastReceiver {
    private static final String TAG = "TimeChangeReceiver";
    @Override
    public void onReceive(Context context, Intent intent) {
        Log.i(TAG, "onReceive: 时间变化");
        getServiceTime();
    }


    /**
     * 获取服务端时间
     */
    private void getServiceTime() {
        RetrofitManager.getInstance().createRequest(UserService.class)
                .getServiceTime()
                .enqueue(new Callback<CommonResult>() {
                    @Override
                    public void onResponse(Call<CommonResult> call, Response<CommonResult> response) {
                        CommonResult result = response.body();
                        if (!response.isSuccessful() || result == null) {
                            Log.e(TAG, "onResponse: 请求失败");
                            return;
                        }
                        if (result.isError()) {
                            Log.e(TAG, "onResponse: " + result.getMsg());
                        }
                        Log.i(TAG, "onResponse: 获取系统时间完成" + result.getData() + "本地时间：" + System.currentTimeMillis());
                        MyApplication.timeOffset = (long) result.getData() - System.currentTimeMillis();
                    }

                    @Override
                    public void onFailure(Call<CommonResult> call, Throwable t) {
                        Log.e(TAG, "onFailure: 获取系统时间失败");
                        t.printStackTrace();
                    }
                });
    }
}
